package mammuthus.controller.app

import net.csdn.annotation.rest.{State, BasicInfo, At}
import net.csdn.modules.http.{ApplicationController, ViewType, RestRequest}
import net.liftweb.{json => SJSon}
import mammuthus.tool.Constants
import com.google.inject.Inject
import mammuthus.master.Master
import net.csdn.common.env.Environment

import mammuthus.controller.filter.{TokenAuthFilter, AppNameCheckFilter}

/**
  * 6/9/15 WilliamZhu(allwefantasy@gmail.com)
  */
class AppFileSystemTaskController @Inject()(akkaMaster: Master, env: Environment) extends ApplicationController with TokenAuthFilter  with AppNameCheckFilter{

   implicit val formats = SJSon.Serialization.formats(SJSon.NoTypeHints)

   val appRootDir = Constants.getAppRootDir

   def appName = param("appName")

  @At(path = Array("/app/log"), types = Array(RestRequest.Method.GET, RestRequest.Method.POST))
  @BasicInfo(
    desc = "查看文件功能",
    state = State.alpha,
    testParams = "?appName=zookeeper&filePath=logs/zookeeper.out",
    testResult = "",
    author = "WilliamZhu",
    email = "allwefantasy@gmail.com"
  )
  def appLog = {
    auth
    val filePath = akkaMaster.findApp(param("appName")) match {
      case Some(i) => i.appInfo.installPath + "/" + param("appName") + "/" + param("filePath", "logs")
      case None => appRootDir + param("appName") + "/" + param("filePath", "logs")
    }

    val appSlaves = akkaMaster.appList
    val slaves = param("slaves", if (appSlaves.contains(appName)) appSlaves(appName).mkString(",") else "")
    val filterStr = param("filter","-")
    val res = akkaMaster.appLog(slaves, filePath, paramAsLong("offset", -1), paramAsInt("size", -1))
    val finalRes = if(filterStr!="-"){
      res.map{f=>
       val msg = f.msg.split("\n").filter{ k=>
          filterStr.r.findFirstIn(k) match {
            case Some(i) => true
            case None => false
          }
          }.mkString("\n")
         f.copy(msg=msg)
        }
      }  else res

    render(SJSon.Serialization.write(finalRes), ViewType.json)
  }

  @At(path = Array("/app/list/log"), types = Array(RestRequest.Method.GET, RestRequest.Method.POST))
  @BasicInfo(
    desc = "查看某个应用目录",
    state = State.alpha,
    testParams = "?appName=zookeeper&filePath=logs",
    testResult = "",
    author = "WilliamZhu",
    email = "allwefantasy@gmail.com"
  )
  def listAppLog = {
    auth
    checkAppName

    val filePath = akkaMaster.findApp(param("appName")) match {
      case Some(i) => i.appInfo.installPath + "/" + param("appName") + "/" + param("filePath", "logs")
      case None => appRootDir + param("appName") + "/" + param("filePath", "logs")
    }

    if (isEmpty(filePath) || filePath.contains("..")) render(400, s"${appName} is not exists")


    val appSlaves = akkaMaster.appList
    val slave = if (appSlaves.contains(appName)) appSlaves(appName).mkString(",") else null
    val slaves = param("slaves", slave)
    val res = akkaMaster.listAppLog(slaves, filePath)
    render(SJSon.Serialization.write(res), ViewType.json)
  }
 }
